package cn.faster.sys.controller;

import cn.faster.common.enums.CodeEnum;
import cn.faster.sys.entity.SysRole;
import cn.faster.sys.entity.SysUser;
import cn.faster.sys.vo.SysRoleVO;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import java.util.*;
import cn.faster.common.result.ResultBean;
import cn.faster.sys.service.SysRoleService;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author xiaoCoder
 * @date 2021-06-10
 */
@RestController
@CrossOrigin
@AllArgsConstructor
@Api(value = "角色表",tags = "角色表")
@RequestMapping("/sys/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @ApiOperation(value = "获取role根据id",notes="")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/getById")
    public ResultBean<?> role(@RequestParam(value = "id") Integer id) throws Exception {

        SysRole sysRole = sysRoleService.getRoleById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(sysRole);
        return resultBean;
    }

    @ApiOperation(value = "获取role列表",notes="")
    @ApiImplicitParam(name = "map", value = "role实体中的参数", dataType ="Map")
    @GetMapping("")
    public ResultBean<?> roleList(@RequestParam(required = false) Map<String, Object> map) throws Exception {

        List<SysRole> sysRoleList = sysRoleService.listRole(map);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(sysRoleList);
        return resultBean;
    }


    @ApiOperation(value = "添加role",notes="新增一条role")
    @ApiImplicitParam(name = "role", value = "role实体中的参数", required = true, dataType = "Role")
    @PostMapping("")
    //@SysLog("添加role")
    public ResultBean<?> roleAdd(@RequestBody SysRoleVO sysRoleVO) throws Exception {
        if (StringUtils.isEmpty(sysRoleVO)) {
            return ResultBean.VALIDATE(CodeEnum.PARAMETER_FORMAT_DOES_NOT_MATCH);
        }
        return ResultBean.SUCCESS(sysRoleService.addRole(sysRoleVO));
    }

    @ApiOperation(value = "修改role",notes="根据id修改role")
    @ApiImplicitParam(name = "role", value = "role实体", required = true, dataType = "Role")
    @PutMapping("")
    public ResultBean<?> roleUpdate(@RequestBody SysRole sysRole) throws Exception {

        Integer flag = sysRoleService.updateRole(sysRole);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }
    @ApiOperation(value = "物理删除role",notes="根据id物理删除role")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public ResultBean<?> roleDelete(@PathVariable(name = "id") Integer id) throws Exception {

        Integer flag = sysRoleService.deleteRoleById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }

    @ApiOperation(value = "逻辑删除role",notes="根据id逻辑删除role")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @PostMapping("/removeById")
    public ResultBean<?> roleRemove(@RequestParam(value = "id") Integer id) throws Exception {

        Integer flag = sysRoleService.removeRoleById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }


}
